load('application');

before(loadReplyMessage, {only: ['show', 'edit', 'update', 'destroy']});

action('reply',function(){
	console.log('reply here');
	msg = req.body.ReplyMessage;
	if(!msg){
		msg = new ReplyMessage;
	}
	msg.userId = 'testuserid';
	msg.replyedUserId = 'replyedUserId';
	
	ReplyMessage.create(msg,function(err,cbmsg){
		if(err){
			flash('error','create replymessage error!');
			render('new', {
                replymessage: replymessage,
                title: 'New replymessage'
            });
		}
		else{
			flash('info', 'ReplyMessage created');
            redirect(path_to.replymessages());
		}
	});
	render('index');
});

action('new', function () {
    this.title = 'New replymessage';
    this.replymessage = new ReplyMessage;
    
    render();
});

action(function create() {
	content = req.body.content;
	console.log('=================='+content);
	msg = new ReplyMessage;
	msg.content = content;
	msg.userId = 'testuserid';
	msg.replyedUserId = 'replyedUserId';
	
	vote = new VoteCount('eat',2);
	console.log(vote);
	votes = [];
	votes[0] = vote;
	votes[1] = vote;
	
	msg.votes = votes;
	
    ReplyMessage.create(msg, function (err, replymessage) {
        if (err) {
            flash('error', 'ReplyMessage can not be created');
            render('new', {
                replymessage: replymessage,
                title: 'New replymessage'
            });
        } else {
            flash('info', 'ReplyMessage created');
            redirect(path_to.replymessages());
        }
    });
});

action(function index() {
    this.title = 'ReplyMessages index';
    ReplyMessage.all(function (err, replymessages) {
    	i = 0;
    	replymessages.forEach(function(replymessage){
    		i++;
    		replymessage.percent = (10+i)+'%';
    	});
    	ReplyMessage.prototype.percent = '100%';
        render('new',{
            replymessages: replymessages
        });
    });
});

action(function show() {
    this.title = 'ReplyMessage show';
    render();
});

action(function edit() {
    this.title = 'ReplyMessage edit';
    render();
});

action(function update() {
	console.log('content here');
	console.log(body.ReplyMessage.content);
	
	this.file = new File();
    var tmpFile = req.file;
    console.log('============tmpFile:'+tmpFile);
    //this.file.upload(tmpFile.name, tmpFile.path, function (err) {
    this.file.upload('catnode.pdm', 'D:\catnode', function (err) {
        if (err) {
            console.log(err);
            this.title = 'New file';
            flash('error', 'File can not be created');
            //render('new');
        } else {
            flash('info', 'File created');
            //redirect(path_to.files);
        }
    }.bind(this));
    
    this.replymessage.updateAttributes(body.ReplyMessage, function (err) {
        if (!err) {
            flash('info', 'ReplyMessage updated');
            redirect(path_to.replymessage(this.replymessage));
        } else {
            flash('error', 'ReplyMessage can not be updated');
            this.title = 'Edit replymessage details';
            render('edit');
        }
    }.bind(this));
});

action(function destroy() {
    this.replymessage.destroy(function (error) {
        if (error) {
            flash('error', 'Can not destroy replymessage');
        } else {
            flash('info', 'ReplyMessage successfully removed');
        }
        send("'" + path_to.replymessages() + "'");
    });
});

function loadReplyMessage() {
    ReplyMessage.find(params.id, function (err, replymessage) {
        if (err || !replymessage) {
            redirect(path_to.replymessages());
        } else {
            this.replymessage = replymessage;
            next();
        }
    }.bind(this));
}
